package com.dove.controller;
/**
 * 改进
 */

import com.dove.common.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dove.entity.User;
import com.dove.service.IUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import com.dove.service.ICourseService;
import com.dove.entity.Course;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dove
 * @since 2022-03-31
 */
@RestController
@RequestMapping("/courses")
public class CourseController {

    @Resource
    private ICourseService courseService;

    @Resource
    private IUserService userService;

    @PostMapping
    public Result save(@RequestBody Course course) {
        boolean flag = courseService.saveOrUpdate(course);
        if (flag) {
            return Result.success();
        }
        return Result.error();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean flag = courseService.removeById(id);
        if (flag) {
            return Result.success();
        }
        return Result.error();
        //return courseService.removeById(id);
    }

    //删除选中
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        boolean flag = courseService.removeByIds(ids);
        if (flag) {
            return Result.success();
        }
        return Result.error();
    }

    @GetMapping
    public Result findAll() {
        List<Course> list = courseService.list();
        if (list == null) {
            return Result.error();
        }
        return Result.success(list);
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        Course course = courseService.getById(id);
        if (course == null) {
            return Result.error();
        }
        return Result.success(course);
    }

    /*//老版
    @GetMapping("/page")
    public Page<Course> findPage(@RequestParam Integer pageNum,
                                    @RequestParam Integer pageSize) {
        return courseService.page(new Page<>(pageNum, pageSize));
    }*/

   /* @GetMapping("/page")//1对1 方法一
    public Result findPageBy(@RequestParam Integer pageNum,
                             @RequestParam Integer pageSize,
                             @RequestParam(defaultValue = "") String name) {
        IPage<Course> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Course> qw = new QueryWrapper<>();
        if (!"".equals(name)) {
            qw.like("name", name);
        }
        //多表级联方法一 返回page1里有记录，获取teacherId，查询对应老师昵称，加入返回数据
        IPage<Course> page1 = courseService.page(page, qw);
        List<Course> records = page1.getRecords();
        for (Course record : records) {
            User user = userService.getById(record.getTeacherId());
            if (user != null) {
                record.setTeacher(user.getNickname());
            }
        }
        return Result.success(page1);
    }*/
    //1对1 方法二
    @GetMapping("/page")
    public Result findPageBy(@RequestParam Integer pageNum,
                             @RequestParam Integer pageSize,
                             @RequestParam(defaultValue = "") String name) {
        IPage<Course> page = courseService.myFindPage(new Page<>(pageNum, pageSize), name);

        return Result.success(page);
    }

    /**
     * 教师id对应课程TeacherId查询授课list
     * @param id 教师id，不可能为null
     * @return
     */
    @GetMapping("/teacher/{id}")
    public Result findCourseByTeacherId(@PathVariable Integer id){
        List<Course> list = courseService.findCourseByTeacherId(id);
        if (list == null) {
            //list为空即TeacherId为空
            return Result.error("该教师暂不授课");
        }
        return Result.success(list);
    }

    @PostMapping("/studentCourse/{studentId}/{courseId}")
    public Result chooseCourse(@PathVariable Integer studentId,@PathVariable Integer courseId){
        courseService.chooseCourse(studentId, courseId);
        return Result.success();
    }
}
